//
//  YSHomeCardHeaderView.m
//  Yueshi
//
//  Created by yueshi_tech on 15/7/17.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  所有卡片模板的头View

#import "YSHomeCardHeaderView.h"
#import "YSCardMoreCtrl.h"

@interface YSHomeCardHeaderView()
@property (strong, nonatomic) NSTimer *countDownTimer;
@property (weak, nonatomic) IBOutlet UILabel *countDownLabel;
@property (strong, nonatomic) UIImageView *titleIconView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIButton *moreBtn;

@end

@implementation YSHomeCardHeaderView

+(instancetype)headerView{
    return [[[NSBundle mainBundle]loadNibNamed:@"YSHomeCardHeaderView" owner:nil options:nil]lastObject];
}

-(void)awakeFromNib{
    UIImageView *titleIconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, (58+10-21)/2, 21, 21)];
    [self addSubview:titleIconView];
    self.titleIconView = titleIconView;
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, (58+10-21)/2, SCREENW/2, 23)];
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    NSString *more = @"更多";
    CGSize moreSize = [more sizeWithFont:SYS_FONT(13)];
    UIImage *moreImage = [UIImage imageNamed:@"btn_in"];
    
    UIButton *moreBtn = [[UIButton alloc] init];
    [moreBtn.titleLabel setFont:SYS_FONT(13)];
    [moreBtn setTitle:more forState:UIControlStateNormal];
    [moreBtn setTitleColor:publicTextColor forState:UIControlStateNormal];
    [moreBtn setImage:moreImage forState:UIControlStateNormal];
    
    CGSize moreImageSize = CGSizeMake(moreImage.size.width + 3, moreImage.size.height);
    moreSize = CGSizeMake(moreSize.width +2, moreSize.height);
    
    [moreBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -moreImageSize.width, 0, moreImageSize.width)];
    [moreBtn setImageEdgeInsets:UIEdgeInsetsMake(0, moreSize.width, 0, -moreSize.width)];
    
    moreBtn.frame = CGRectMake(SCREENW - 10 - moreSize.width - moreImageSize.width, self.countDownLabel.y, moreSize.width + moreImageSize.width, moreSize.height + moreImageSize.height);
    
    [self addSubview:moreBtn];
    _moreBtn = moreBtn;
    [moreBtn addTarget:self action:@selector(moreClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setSuperCurrentY:(CGFloat)superCurrentY{
    if (!superCurrentY) {
        self.height = CARD_HEADERVIEW_HEIGHT - 10;
        self.titleIconView.y = (CARD_HEADERVIEW_HEIGHT - 10-21)/2;
        self.titleLabel.y = self.titleIconView.y;
        self.moreBtn.y = (CARD_HEADERVIEW_HEIGHT - 10 - self.moreBtn.height)/2;
    }
}

-(void)setInfo:(YSHeaderResult *)info{
    _info = info;
    if(ISBLANK(info.cardIcon)){
        [self.titleIconView removeFromSuperview];
        [self.titleLabel setX:YSPublicXInset];
        [self.titleLabel setText:info.cardName];
    }else{
        [self.titleIconView setX:YSPublicXInset];
        [self.titleIconView setImageURLStr:info.cardIcon];
        [self.titleLabel setText:info.cardName];
        [self.titleLabel setX:CGRectGetMaxX(self.titleIconView.frame)+YSPublicXInset];
    }
    if(info.endTime != 0){
        NSTimeInterval endTime =[TimeUtils getRemainTime:info.endTime];
        NSString *timeStr = [NSString stringWithFormat:@"%@",[TimeUtils getFormatRemainTime:endTime]];
        NSString *formatTimeStr =  [NSString stringWithFormat:@"仅剩%@",timeStr];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:formatTimeStr];
        [str addAttribute:NSForegroundColorAttributeName value:ThemeColor range:NSMakeRange(2,timeStr.length)];
        [self.countDownLabel setAttributedText:str];
        
        
        _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeLabel) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_countDownTimer forMode:NSDefaultRunLoopMode];
    }
    NSLog(@"ismore = %ld",info.isMore);
    [self.moreBtn setHidden: !info.isMore];
}

-(void)timeLabel{
   NSTimeInterval reMainTime = [TimeUtils getRemainTime:_info.endTime];
    if(reMainTime <= 0){
        [_countDownTimer invalidate];
        //在这写倒计时完了之后需要做的事情
    }else{
        NSString *timeStr = [NSString stringWithFormat:@"%@",[TimeUtils getFormatRemainTime:[TimeUtils getRemainTime:_info.endTime]]];
        NSString *formatTimeStr =  [NSString stringWithFormat:@"仅剩%@",timeStr];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:formatTimeStr];
        [str addAttribute:NSForegroundColorAttributeName value:ThemeColor range:NSMakeRange(2,timeStr.length)];
        [self.countDownLabel setAttributedText:str];
    }
}

-(void)invalidateTimer{
    [_countDownTimer invalidate];
}

- (void)moreClick:(UIButton *)sender{
    [[self getViewController:self].navigationController pushViewController:[[YSCardMoreCtrl alloc] initWithCardId:self.info.id cardName:self.info.cardName] animated:YES];
}

- (UIViewController *)getViewController:(UIView *)view{
    if (view.superview.viewController) {
        return view.superview.viewController;
    }
    
    return [self getViewController:view.superview];
    
}

@end
